/*
 * Created by Federico Pisarello - SisDev Software (c) 2013 - fpisarello@sisdevsoft.com
 */

var popForm = 'chequesPop.php';
var actionForm = 'chequesAction.php';

var joDataCheque;
var oTable;

/** Variable of tracking the IndexPK **/
var actionPK = undefined;

function $_init()
{	
	var gaiSelected;
	
	$(document).ready(function() {
		oTable = $('#dt_cheques');
    	oTable.dataTable( {
					"bJQueryUI": true,
					"sPaginationType": "two_button",
					"bInfo": false,
					"bLengthChange": false,
					"bProcessing": true,
					"bAutoWidth": false,
					"bServerSide": true,
					"sAjaxSource": actionForm + "?accion=search",
					"fnRowCallback": function( nRow, aData, iDisplayIndex ) {
						if ( jQuery.inArray(aData[0], gaiSelected) != -1 )
						{
					        $(nRow).addClass('row_selected');
						}
						return nRow;
					},
					"aoColumns": [
					  			null,
					  			null,
					  			null,
					  			null,
					  			null,
					  			null
					  		],
			        fnPreDrawCallback: function () {
			            // Initialize the responsive datatables helper once.
			            if (!responsiveHelper) {
			                responsiveHelper = new ResponsiveDatatablesHelper(oTable, breakpointDefinition);
			            }
			        },
			        fnRowCallback  : function (nRow) {
			            responsiveHelper.createExpandIcon(nRow);
			        },
			        fnDrawCallback : function (oSettings) {
			            responsiveHelper.respond();
			        }
				});
    	
    	/* Click event handler */
    	$('#dt_cheques tbody').on('click', 'tr', function () {
    		var aData = oTable.fnGetData(this);
    		var iId = aData[0];
    		actionPK = aData[0];
    		
    		gaiSelected =  [];
    		if ( $(this).hasClass('row_selected') ) {
	            $(this).removeClass('row_selected');
	            actionPK = undefined;
	        }
	        else {
	            oTable.$('tr.row_selected').removeClass('row_selected');
	            $(this).addClass('row_selected');
	            gaiSelected[gaiSelected.length++] = iId;
	        }
    	} );

	});
	
	$("#chequesDialog").dialog({
		autoOpen:false, 
		height:'auto', 
		width:400, 
		modal: true,
		closeOnEscape: false,
		position : ['center',10],		
		dialogClass:'no-close',
		beforeClose: function() { $_ClearChequesPopForm(); },
		open: function() {
			$('#clientesDialog').css('background-color','#eee');
		}
	});
		
	$('#btnDetalle').click( function(e) {
		detalleProxyShow();
	});
	
	$('#btnExcel').click( function(e) {
		
	});
}

function $_initPop()
{
	//Add Click for All Buttons in Pop Form
	$('#btnCerrar').click( function(e) {
		$('#chequesDialog').dialog('close');
	});

	$("#nroRecibo").val(joDataCheque.nroRecibo);
	$("#fechaEmisionCheque").val(joDataCheque.fechaEmisionCheque);
	$("#fechaVtoCheque").val(joDataCheque.fechaVtoCheque);
	$("#importeCheque").val(joDataCheque.importeCheque);
	$("#sucursalCheque").val(joDataCheque.sucursalCheque);
	$("#bancoCheque").val(joDataCheque.bancoCheque);
	$("#nroCheque").val(joDataCheque.nroCheque);
	$("#firmanteCheque").val(joDataCheque.firmanteCheque);
	$("#cuitFirmanteCheque").val(joDataCheque.cuitFirmanteCheque);
}

function detalleProxyShow()
{
	if(actionPK === undefined)
	{
		$_showMessage('ALERT', 'ALERTA', 'Seleccione un Cheque de la Grilla');
		return;
	}
	
	var paramData =  "accion=" + iConst.CONSULT + "&idCheque=" + actionPK;
	
    $.ajax({
        type: 'POST',
        url: actionForm,
        data: paramData,
		dataType: 'json',
        success: function(jsonObj)
                            {
        						joDataCheque = jsonObj;
        						//Open Pop Dialog
        						$('#chequesDialog').load(popForm).dialog('open');
                            }
        });
	
return true;
}

function $_ClearChequesPopForm()
{	
	$("#chequesDialog").empty();
}